from django.shortcuts import render
from django.http import HttpRequest, JsonResponse
from django.views.decorators.csrf import csrf_exempt
import package.data_operation as logic

# 与浏览器交互文件，显示想要显示的内容

def go_home(request: HttpRequest):
    return render(request, "home.html")

# 获取表数据
def getAllData(request: HttpRequest):
    return JsonResponse(logic.display_goods(), safe=False)

# 添加商品信息
@csrf_exempt
def add_goods(request: HttpRequest):
    add_name = request.POST['name'];
    add_unit = request.POST['unit'];
    add_price = request.POST['price'];
    logic.add_goods(add_name, add_unit, float(add_price))
    return JsonResponse(None, safe=False)

# 修改商品信息
@csrf_exempt
def alter_goods(request: HttpRequest):
    alter_ID = request.POST['ID'];
    alter_name = request.POST['name'];
    alter_unit = request.POST['unit'];
    alter_price = request.POST['price'];
    return JsonResponse(logic.alter_goods(alter_ID, alter_name, alter_unit, alter_price), safe=False)

# 删除商品信息
@csrf_exempt
def delete_goods(request: HttpRequest):
    delete_ID = request.POST['ID'];
    return JsonResponse(logic.delete_goods(int(delete_ID)), safe=False)